#!/usr/bin/env python3
#coding:utf8
import os
import shutil

html = """ """
def toHtml(sample_date, GScode, phone, first_name, last_name, dob, price, email):
    fullname = first_name + "_" + last_name 
    filename = fullname + " " + email + ".htm"
    with open(filename, 'w') as fs:
        fs.write(html)

    if not os.path.exists("tmp"):
        os.makedirs('tmp')
    shutil.copy2(filename, os.path.join('tmp', filename))

    dirname = "out/"+sample_date
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    os.rename(filename, os.path.join(dirname, filename))

def Gen_HTML(sample_date, GScode, first_name, last_name, dob, phone, price, email):
    price = "$" + price
    tmp = sample_date.split('/')
    sdate = "/".join([tmp[1], tmp[2], tmp[0]])
    tel = [d for d in phone if d.isdigit()]
    if len(tel) == 10:
        telephone =  "(" + tel[0] + tel[1] + tel[2] + ")" + tel[3]+ tel[4]+ tel[5] + '-' + tel[6]+ tel[7]+ tel[8] + tel[9]
    else:
        telephone  = phone
    
    info = {
           1332: sdate, #sample date
           1335: GScode,
           2128: first_name + " " + last_name,
           2130: telephone,
           2335: first_name + " " + last_name,
           2338: dob,
           2341: price,
           2345: price,
           2411: price}

    fs = open('doc/invoice.htm', 'r', encoding='utf8')
    lines = fs.readlines()
    for line, content in info.items():
        lines[line - 6] = content
    fs.close()
    
    fullname = first_name + "_" + last_name 
    filename = fullname + "-" + email + ".htm"
    with open(filename, 'w', encoding='utf8') as fs:
        fs.writelines(lines)
    
    #copy to tmp
    if not os.path.exists("tmp"):
        os.makedirs('tmp')
    shutil.copy2(filename, os.path.join('tmp', filename))

    #copy to yyyy/mm/dd
    dirname = "out/"+sample_date
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    os.rename(filename, os.path.join(dirname, filename))



if __name__ == "__main__":
    Gen_HTML("2020/12/10", "GS202020001", "Chang", "Xie", "08/19/1983", "1234567890", "390",   "xiechc@gmail.")
    
